#! /usr/bin/env python

# *------------------------------------------------------------------
# * sample.py  
# *
# * Cisco ONE-P Python SDK
# *
# * Copyright (c) 2011-2013 by Cisco Systems, Inc.
# * All rights reserved.
# *------------------------------------------------------------------
# *

import logging
import os
from onep.element.NetworkElement import NetworkElement
from onep.element.ElementConfig import ElementConfig
from onep.core.util.HostIpCheck import HostIpCheck
from onep.core.exception.OnepIllegalArgumentException \
        import OnepIllegalArgumentException
from onep.vty.VtyService import VtyService
from onep.core.exception.OnepConnectionException import OnepConnectionException

# Must have a Cisco network device capable of communicating
# with ONE-P APIs.  Network Element must also have onep
# configured with the socket transport and have the correct
# onep services activated.  A network interface must be
# configured with a valid IP address and pingable.


def ip_check(elem):
    if not HostIpCheck(elem).is_ipaddress():
        raise OnepIllegalArgumentException('Invalid IP address %s' % elem)


def testapp():
    log = logging.getLogger('onep')
    log.setLevel(logging.DEBUG)

    while True:
        elementIP = raw_input('Enter element IP address (10.75.63.7): ')
        if len(elementIP) > 0:
            try:
                ip_check(elementIP)
                break
            except OnepIllegalArgumentException, e:
                print e
                continue
        else:
            elementIP = "10.75.63.7"
            break

    appname = raw_input('Enter name of application : ')
    user = raw_input('Enter username : ')
    pwd = raw_input('Enter password : ')

    ne = NetworkElement(elementIP, appname)
    con = ne.connect(user, pwd)
    print 'Connected to host'
    vty = VtyService(ne)
    necfg = ElementConfig(ne)
    print necfg.hostname
    print necfg.domain
    try:
        vty.open()
        vty.write("conf t")
        vty.write("ip domain name cisco.com")
    except OnepConnectionException, e:
        print e
    finally:
        vty.close()

    ne.disconnect()

if __name__=='__main__':
    testapp()
